/**
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2009，所有权利保留。
 * 
 * 项目名：	rumba-widget2
 * 文件名：	RMenuCheckItem.java
 * 模块说明：	
 * 修改历史：
 * Jan 18, 2009 - lxm - 创建。
 */
package com.hd123.rumba.gwt.widget2.client.menu;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHTML;
import com.google.gwt.user.client.ui.ListenerWrapper;
import com.google.gwt.user.client.ui.SourcesClickEvents;
import com.google.gwt.user.client.ui.Widget;
import com.hd123.rumba.gwt.widget2.client.action.IsRCheckActionControl;
import com.hd123.rumba.gwt.widget2.client.action.RCheckAction;

/**
 * 具有选中/不选中状态的菜单项。
 * <p>
 * 
 * <b>CSS Style Rules<b>
 * <ol type=disc>
 * <li>.rb-PopupMenu .rb-MenuCheckItem { 整个菜单项，正常状态 }</li>
 * <li>.rb-PopupMenu .rb-MenuCheckItem-selected { 整个菜单项，选中状态 }</li>
 * <li>.rb-PopupMenu .rb-MenuCheckItem-disabled { 整个菜单项，不可用状态 }</li>
 * <li>.rb-PopupMenu .rb-MenuCheckItem .uncheckedIcon { 菜单项的图标，未被选中状态 }</li>
 * <li>.rb-PopupMenu .rb-MenuCheckItem .checkedIcon { 菜单项的图标，被选中状态 }</li>
 * <li>.rb-PopupMenu .rb-MenuCheckItem .disabledUncheckedIcon {
 * 菜单项的图标，未被选中且不可用状态 }</li>
 * <li>.rb-PopupMenu .rb-MenuCheckItem .disabledCheckedIcon {
 * 菜单项的图标，被选中且不可用状态 }</li>
 * <li>.rb-PopupMenu .rb-MenuCheckItem .seperator { 菜单项的图标与标题之间的分隔线 }</li>
 * <li>.rb-PopupMenu .rb-MenuCheckItem .caption { 菜单项的标题部分 }</li>
 * </ol>
 * 
 * @author lxm
 * 
 */
@SuppressWarnings("deprecation")
public class RMenuCheckItem extends Composite implements HasHTML, IsRMenuItem,
    IsRCheckActionControl, SourcesClickEvents, HasClickHandlers {

  private static final String STYLENAME_DEFAULT = "rb-MenuCheckItem";

  /**
   * 创建对象实例。
   */
  public RMenuCheckItem() {
    super();
    drawSelf();
  }

  /**
   * 创建对象实例。
   * 
   * @param action
   */
  public RMenuCheckItem(RCheckAction action) {
    this();
    setAction(action);
  }

  public RMenuCheckItem(String text) {
    this();
    setText(text);
  }

  /**
   * @deprecated 被{@link #RMenuCheckItem(String, ClickHandler)}替代。
   */
  public RMenuCheckItem(String text, ClickListener listener) {
    this(text);
    addClickListener(listener);
  }
  
  public RMenuCheckItem(String text, ClickHandler handler) {
    this(text);
    addClickHandler(handler);
  }

  private RPopupMenu parentMenu;
  private boolean enabled = true;
  private boolean checked = false;
  private RCheckAction action;
  private Grid grid;
  private HTML htmlCaption;

  public String getHTML() {
    return htmlCaption.getHTML();
  }

  public void setHTML(String html) {
    htmlCaption.setHTML(html);
  }

  public String getText() {
    return htmlCaption.getText();
  }

  public void setText(String text) {
    htmlCaption.setText(text);
  }

  public RPopupMenu getParentMenu() {
    return parentMenu;
  }

  public void setParentMenu(RPopupMenu menu) {
    parentMenu = menu;
  }

  public Widget toWidget() {
    return this;
  }

  public boolean isOrHasChild(Element element) {
    return DOM.isOrHasChild(getElement(), element);
  }

  public boolean asSeperator() {
    return false;
  }

  public RPopupMenu getSubMenu() {
    return null;
  }

  public Widget getContextWidget() {
    return parentMenu == null ? null : parentMenu.getContextWidget();
  }

  public void onClick() {
    setChecked(!checked);
//    listeners.fireClick(this);
    if (parentMenu != null)
      parentMenu.hide();
  }

  public void onMouseOver() {
    // Do Nothing
  }

  public void onMouseOut() {
    // Do Nothing
  }

  public void setSelectionStyle(boolean selected) {
    if (enabled) {
      if (selected)
        addStyleDependentName("selected");
      else
        removeStyleDependentName("selected");
    }
  }

  public RCheckAction getAction() {
    return action;
  }

  public void setAction(RCheckAction action) {
    if (action == null) {
      if (this.action != null)
        this.action.removeControl(this);
    }

    this.action = action;

    if (this.action != null) {
      if (this.action.containsControl(this))
        return;
      this.action.addControl(this);
    }
  }

  public void applyAction() {
    if (action == null)
      return;
    setHTML(action.getCaption());
    setTitle(action.getTitle());
    setEnabled(action.isEnabled());
    setChecked(action.isChecked());
  }

  public void applyChecked() {
    setChecked(action.isChecked());
  }

  /**
   * @deprecated 被{@link #addClickListener(ClickHandler)}替代。
   */
  public void addClickListener(ClickListener listener) {
    ListenerWrapper.WrappedClickListener.add(this, listener);
  }

  /**
   * @deprecated 被由{@link #addClickListener(ClickHandler)}所返回的
   *             {@link HandlerRegistration#removeHandler()}替代。
   */
  public void removeClickListener(ClickListener listener) {
    ListenerWrapper.WrappedClickListener.remove(this, listener);
  }

  public HandlerRegistration addClickHandler(ClickHandler handler) {
    return addDomHandler(handler, ClickEvent.getType());
  }

  public boolean isEnabled() {
    return enabled;
  }

  public void setEnabled(boolean enabled) {
    this.enabled = enabled;
    refreshIcon();
    if (enabled)
      DOM.removeElementAttribute(htmlCaption.getElement(), "disabled");
    else
      DOM.setElementAttribute(htmlCaption.getElement(), "disabled", "true");
  }

  public boolean isChecked() {
    return checked;
  }

  public void setChecked(boolean checked) {
    this.checked = checked;
    refreshIcon();
  }

  public void setVisible(boolean visible) {
    super.setVisible(visible);
    if (parentMenu != null)
      parentMenu.onItemSetVisible(this);
  }

  private void drawSelf() {
    grid = new Grid(1, 3);
    grid.setCellPadding(0);
    grid.setCellSpacing(0);
    grid.setWidth("100%");

    grid.setWidget(0, 0, new HTML(""));
    refreshIcon();

    grid.getCellFormatter().setStyleName(0, 1, "seperator");

    htmlCaption = new HTML("");
    grid.setWidget(0, 2, htmlCaption);
    grid.getCellFormatter().setStyleName(0, 2, "caption");

    initWidget(grid);
    setStyleName(STYLENAME_DEFAULT);

    addClickHandler(new Handler_self());
  }

  private void refreshIcon() {
    if (enabled) {
      if (checked)
        grid.getCellFormatter().setStyleName(0, 0, "checkedIcon");
      else
        grid.getCellFormatter().setStyleName(0, 0, "uncheckedIcon");
    } else {
      if (checked)
        grid.getCellFormatter().setStyleName(0, 0, "disabledCheckedIcon");
      else
        grid.getCellFormatter().setStyleName(0, 0, "disabledUncheckedIcon");
    }
  }
  
  private class Handler_self implements ClickHandler {
    public void onClick(ClickEvent event) {
      if (action != null) {
        action.setChecked(checked);
        action.fireClick();
      }
    }
    
  }

}
